﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Kevin.Framework.Data.OrmMapping;

namespace Kevin.WindosControls.Test
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm1()
        {
            InitializeComponent();

            List<UIItem> items = new List<UIItem>();
            items.Add(new UIItem() {ID=1,Text="Age",OrderIndex=3,PropertyName="Age" });
            items.Add(new UIItem() { ID = 1, Text = "Name", OrderIndex = 1, PropertyName = "Name" });
            items.Add(new UIItem() { ID = 1, Text = "Value", OrderIndex = 4, PropertyName = "Value" });
            items.Add(new UIItem() { ID = 1, Text = "Sex", OrderIndex = 2, PropertyName = "Sex" });

            foreach (UIItem item in items) {
                dynamicLoad(item);
            }

            this.dataBind1.Data = new Person();
            this.button1.Click += new EventHandler(button1_Click);
        }

        void button1_Click(object sender, EventArgs e)
        {
            if (this.dataBind1.ValidateSuccess)
            {
                DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider provider = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider();
                
                Person result = (Person)this.dataBind1.Data;
                MessageBox.Show(result.Name + ":" + result.Age.ToString());
            }
            layoutControl1.SaveLayoutToXml("test1");
        }

        private void dynamicLoad(UIItem item)
        {
            Control ctr = new TextBox() { Name = "test" + item.ID.ToString() };
            DevExpress.XtraLayout.LayoutControlItem Citem =layoutControlGroup1.AddItem();
            Citem.Text = item.Text;
            Citem.Control = ctr;
            Citem.TextAlignMode = DevExpress.XtraLayout.TextAlignModeItem.UseParentOptions;
            //this.layoutControl1.AddItem(
            //Citem.Visibility= DevExpress.XtraLayout.Utils.LayoutVisibility
            this.dataBind1.DataBindItems.Add(new Controls.DataBindItem() { TargetControl=ctr, PropertyName=item.PropertyName });
        }

        public class UIItem
        {
            public int ID { get; set; }
            public string Text { get; set; }
            public int OrderIndex { get; set; }
            public string PropertyName { get; set; }

        }

        public EntityBase Entity
        {
            get;
            set;
        }

        private void XtraForm1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void XtraForm1_Activated(object sender, EventArgs e)
        {
            Form1.RigisterCurrentForm(this);
        }

    }
}